package com.rams.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.json.simple.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.rams.domain.vo.ItemVO;
import com.rams.domain.vo.UserExpenditure;
import com.rams.service.UserService;
import com.rams.service.vo.UserReportVO;
import com.rams.service.vo.DisplayVO;

@Controller
public class CircleController {
	
	@Autowired
	UserService userService;

	@RequestMapping(value = "/circle/{circleId}", method = {RequestMethod.POST, RequestMethod.GET})
    public ModelAndView loginUser(HttpSession session, HttpServletRequest request, 
    		@PathVariable(value="circleId") String circleId) {
    	System.out.println("RAMSController.loginUser()"+ circleId);

    	String userId = (String)session.getAttribute("userId");
    	ModelAndView modelAndView = new ModelAndView("usercircle");
    	DisplayVO displayVO = null;
    	if(null != userId && !userId.equals("")){
    		displayVO = userService.getCircleData(circleId, userId);
    	}
//    	CirclePieChartVO circlePieData = new CirclePieChartVO();
//    	circlePieData.setUserexpense(displayVO.getCircleExpenditurePerUser());
    	
    	JSONArray json = new JSONArray();
    	json.addAll(displayVO.getCircleExpenditurePerUser());
    	
    	modelAndView.addObject("jsonData", json.toJSONString());
     	modelAndView.addObject("displayVO", displayVO);
        return modelAndView;
    }
	
	@RequestMapping(value = "/{circleId}/additem", method = {RequestMethod.POST, RequestMethod.GET})
    public ModelAndView getDataToAddItem(HttpSession session, HttpServletRequest request, 
    		@PathVariable(value="circleId") String circleId) {
    	System.out.println("CircleController.addItem()");

    	String userId = (String)session.getAttribute("userId");
    	ModelAndView modelAndView = new ModelAndView("additem");
    	ItemVO item = new ItemVO();
    	item.setUser(userId);
    	if(null != userId && !userId.equals("")){
    		item = userService.getItemData(circleId, item);
    	}
    	modelAndView.addObject("itemvo", item);
        return modelAndView;
    }
	
	@RequestMapping(value = "/additem", method = {RequestMethod.POST, RequestMethod.GET})
    public ModelAndView addItem(ItemVO itemVO, HttpSession session, HttpServletRequest request
    		) {
    	System.out.println("CircleController.addItem()");

    	ModelAndView modelAndView = new ModelAndView("additem");
    	userService.addItemData(itemVO);
    	modelAndView.addObject("itemvo", itemVO);
        return modelAndView;
    }
	
	@RequestMapping(value = "/{circleID}/{username}", method = {RequestMethod.GET})
    public ModelAndView getUserReport(@PathVariable("circleID") String circleID, 
    		@PathVariable("username") String userName,
    		HttpServletRequest request) {
	
		ModelAndView model = new ModelAndView("userItems");
		model.addObject("report", userService.getUserItemsReport(circleID, userName));
		
		return model;
    	
    }
	
}
